package com.xsf.spring.cloud.alibaba.consumer.ribbon;

import com.sun.javafx.binding.StringFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 通过Ribbon方式访问服务提供者,需要配置restTemplate
 */
@RestController
public class RibbonController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/com.xsf.spring.cloud.alibaba.sentinel.consumer/{name}")
    public String echo(@PathVariable("name") String name) {
        ServiceInstance instance = this.loadBalancerClient.choose("nacos-provider");
        String url = instance.getUri() + "/echo/" + name;
        return this.restTemplate.getForObject(url, String.class);
    }

}
